package Database;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

import klassen.DbVerbindung;

public class MethodsSQL {

	/**
	 * send a SQL statement to request data
	 * @param query SQL statement
	 * @return ResultSet with the requested data
	 */
	public ResultSet sendQuery(String query) {

		ResultSet rs = null;
		
		try {

			DbVerbindung dbverbindung = new DbVerbindung();
			Connection conn = dbverbindung.getConnection();
			Statement stmt = conn.createStatement();
			rs = stmt.executeQuery(query);

		} catch (Exception e) {
			System.out.println("SQL ERROR when sending query: " + e);
		}
		
		return rs;
	}
	
	/**
	 * sends a Query to the database and returns a filled ResultSet with the requested data
	 * @param query SQL statement
	 * @param input the values which are filled in the wildcard spots with their position as an integer 
	 * @param numberOfWildcards how many wildcards are used in the sql statement
	 * @return ResultSet with the requested data
	 * @throws SQLException
	 */
	public ResultSet sendQuery(String query, HashMap<String, Integer> input, Integer numberOfWildcards) throws SQLException {
		
		DbVerbindung dbverbindung = new DbVerbindung();
		Connection conn = dbverbindung.getConnection();
		ResultSet rs = null;

		try {

			PreparedStatement stmt = conn.prepareStatement(query);
			
			for (Map.Entry<String, Integer> e : input.entrySet()) {
//	            for (int x = 0; x<numberOfQuestionMarks; x++) {
	            	stmt.setString(e.getValue().intValue(), e.getKey());
//	            }
			}
			
			rs = stmt.executeQuery(query);

		} catch (Exception e) {
			System.out.println("SQL ERROR when sending query: " + e);
		}
		return rs;
	}

	/**
	 * executes update
	 * @param query SQL statement
	 */
	public void executeUpdate(String query){

		try {

			DbVerbindung dbverbindung = new DbVerbindung();
			Connection conn = dbverbindung.getConnection();
			Statement stmt = conn.createStatement();
			stmt.executeQuery(query);
			
		} catch (Exception e) {
			System.out.println("SQL ERROR when sending update: " + e);
		}
	}
	
	/**
	 * executes an prepared statement
	 * @param query SQL statement with ? as wildcards
	 * @param input the values which are filled in the wildcard spots with their position as an integer
	 * @param numberOfWildcards how many wildcards are used in the passed on SQL statement
	 * @throws SQLException
	 */
	public void executeUpdate(String query, HashMap<String, Integer> input, Integer numberOfWildcards) throws SQLException {
		
		DbVerbindung dbverbindung = new DbVerbindung();
		Connection conn = dbverbindung.getConnection();

		try {

			PreparedStatement stmt = conn.prepareStatement(query);
			
			for (Map.Entry<String, Integer> e : input.entrySet()) {
				int where = e.getValue();
//	            for (int x = 1; x<numberOfWildcards+1; x++) {
	            	stmt.setString(where, e.getKey());
//	            }
			}
			stmt.executeUpdate();
			
		} catch (Exception e) {
			System.out.println("SQL ERROR when sending query: " + e);
		}
	}
}
